﻿using Huirui.Cavan.Domain.MainBoundedContext.SalesModule;
using Huirui.Cavan.Domain.MainBoundedContext.SearchModule;
using System;
using System.Collections;
using Huirui.Cavan.Application.MainBoundedContext.SystemModule;
using Huirui.Cavan.Domain.Seedwork;
using Huirui.Cavan.Domain.MainBoundedContext.SystemModule;
using Huirui.Cavan.Domain.MainBoundedContext.FinanceModule;
using Huirui.Cavan.Core.Extension;

namespace Huirui.Cavan.Application.MainBoundedContext.SalesModule
{
	#region Receipt

    public class ReceiptAppService : BaseAppService<Receipt, string, ReceiptSearch, IReceiptRepository>, IReceiptAppService
	{
        private IReceivablesAppService ReceivablesAppService { get; set; }
        private IAccountAppService AccountAppService { get; set; }
        private ICustomerAppService CustomerAppService { get; set; }

        public override Domain.Seedwork.BoolGenericResult<Receipt> AddOrUpdate(Receipt entity)
        {
            Init();
            BoolGenericResult<Receipt> result = new BoolGenericResult<Receipt>();

            this.ReceivablesAppService = this.SpringContext.GetObject(SalesModuleConst.ECEIVABLESAPPSERVICE) as IReceivablesAppService;
            this.AccountAppService = this.SpringContext.GetObject(SystemModuleConst.ACCOUNTAPPSERVICE) as IAccountAppService;
            this.CustomerAppService = this.SpringContext.GetObject(SystemModuleConst.CUSTOMERAPPSERVICE) as ICustomerAppService;
            Employee employee = this.EmployeeAppService.GetSingleBySearch(SearchFactory.CreateEmployeeSearch(SecurityAppService.UserAccount.Id));
            Accountant accountant = new Accountant(employee);

            Receipt oldReceipt = null;
            if (entity.Id.IsNotEmpty())
            {
                oldReceipt = this.Repository.Get(entity.Id);
            }
            if (entity.ToAccount.IsNotNull() && entity.ToAccount.Id.IsNotEmpty())
            {
                entity.ToAccount = this.AccountAppService.GetById(entity.ToAccount.Id);
            }
            if (entity.Customer.IsNotNull() && entity.Customer.Id.IsNotEmpty())
            {
                entity.Customer = this.CustomerAppService.GetById(entity.Customer.Id);
            }
            foreach (ReceiptDetail rd in entity.ReceiptDetails)
            {
                rd.Receivables = this.ReceivablesAppService.GetById(rd.Receivables.Id);
            }

            if (oldReceipt.IsNull())
            {
                IIDGenerateAppService idGenerateAppService = this.SpringContext.GetObject(SystemModuleConst.IDGENERATEAPPSERVICE, new object[] { employee.Corporation }) as IIDGenerateAppService;
                entity.Id = idGenerateAppService.GenerateID(SystemIdentifier.ReceiptCodeRuleType.Receipt);

                result = accountant.Receive(entity.Id, entity.ReceiptDate, entity.ToAccount, entity.AccountNumber, entity.Amount, entity.Discount, entity.Currency, entity.CurrencyRate, employee, entity.Remark,
                     entity.Customer, entity.ReceiptDetails);
                if (result.IsSuccess)
                {
                    foreach (ReceiptDetail rd in entity.ReceiptDetails)
                    {
                        this.ReceivablesAppService.Update(rd.Receivables);
                    }

                    result = base.Add(result.Object);
                }
            }
            else
            {
            }
            return result;
        }
	}
	#endregion
}